angular.module('home.controller', []).controller('homeController', ['$scope', '$charitySocietyService', '$state',
    function ($scope, $charitySocietyService, $state) {
        $charitySocietyService.getArticleDetail({articleType: 9}, function (datas) {
            $scope.videoList = datas.data;
        }, function (err) {
            console.error(err);
        });

        $scope.eventList = [];

        $scope.selectedType = [10, 11, 12];

        $scope.selectedTab = function (type) {
            if ($scope.selectedType == type) {
                return;
            }
            $scope.selectedType = type;
            $scope.queryArticles($scope.selectedType);
        }

        $scope.articles = [];
        $scope.queryArticles = function () {
            $charitySocietyService.getArticles({
                pageNo: 0,
                articleType: $scope.selectedType,
                numPerPage: 5
            }, function (data) {
                $scope.articles = [];
                if (null != data && data.data && data.data.length > 0) {
                    $scope.articles = data.data;
                }
            }, function (err) {
                console.error(err);
            });
        };

        $charitySocietyService.getHeadBannerSources().then(function(result){
            if(!result || result.code != 0 ){
                logger.logError((result && result.message) || '查询失败');
            }
            var  sources = [];
            _.forEach(result.data, function(source){
                sources.push(source.banner_url);
            });

            $scope.bannerSources = sources;

            setTimeout(function(){
                $('.banner').unslider({
                    autoplay: true,
                    arrows: false
                });

            },100);
        });


        $charitySocietyService.getEventsData(function(data){
            $scope.eventList = data.data;
            if(!$scope.eventList){
                $scope.eventList = [];
            }
        },function(err){

        })

        $scope.donation = function () {
            $state.go('donation');
        };

        $scope.queryArticles($scope.selectedType);

        $charitySocietyService.getCurrentYearDonation().then(function (result) {
            $scope.yearDonation =  result.data.yearTotalMoney && parseFloat(result.data.yearTotalMoney)/100 ;

        });
        $charitySocietyService.getCurrentMonthDonation().then(function (result) {
            $scope.monthDonation =  result.data.monthTotalMoney && parseFloat(result.data.monthTotalMoney)/100 ;


        });
        $charitySocietyService.getAllDonation().then(function (result) {
            $scope.totalDonation =  result.data.totalMoney && parseFloat(result.data.totalMoney/100) ;

        });
        $charitySocietyService.getLatestDonations().then(function (result) {
            $scope.latestDonations = result.data;
        });

    }]);